package net.tbao.seckill.interceptor;

import net.tbao.common.constant.AuthServerContstant;
import net.tbao.common.vo.MenberResponVO;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LonginUserInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MenberResponVO> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        AntPathMatcher antPathMatcher=new AntPathMatcher();
        boolean match = antPathMatcher.match("/kill", requestURI);
        if (match ){
            MenberResponVO attribute = (MenberResponVO) request.getSession().getAttribute(AuthServerContstant.LOGIN_USER);
            if(attribute!=null){
                threadLocal.set(attribute);
                return true;

            }else {
                //未登录状态
                request.getSession().setAttribute("msg","请先登录");
                response.sendRedirect("http://auth.tbao.cn/login.html");
                return false;
            }
        }
        return true;
    }
}
